package test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Calen {
	private GregorianCalendar calendar;

	public Calen() {
		calendar = new GregorianCalendar();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, 0);
	}

	public Calen(int year, int month) {
		this();
		//将日历设置到当月的最后一天
		calendar.set(year, month, 0);
	}

	public static void main(String[] args) {
		Calen calen;
		if(args.length != 2 && args.length !=0){
			System.out.println("输入出错，请输入你要查询的月份，格式为： yyyy MM");
			return ;
		}
		if(args.length == 0){
			calen = new Calen();
		} else {
			int year = Integer.parseInt(args[0]);
			int month = Integer.parseInt(args[1]);
			if(year<=0 || month<=0 || month>12){
				System.out.println("输入出错！");
				return ;
			}
			calen = new Calen(year,month);
		}
		System.out.println();
		System.out.println();
		calen.ShowMonth();
		System.out.println();
		calen.ShowTime();
	}

	public void ShowMonth() {
		System.out.println("\t\t\t" + calendar.get(Calendar.YEAR) + "年"
				+ (calendar.get(Calendar.MONTH) + 1) + "月");
		int days = calendar.get(Calendar.DAY_OF_MONTH);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		int i, j, count = 7 - (days - dayOfWeek) % 7;
		System.out.println();
		System.out.print("\tS");
		System.out.print("\tM");
		System.out.print("\tT");
		System.out.print("\tW");
		System.out.print("\tT");
		System.out.print("\tF");
		System.out.print("\tS");
		System.out.println();
		for (i = 0; i < count; ++i) {
			System.out.print("\t");
		}
		for (j = 1; j <= 10; ++i, ++j) {
			if (0 == i % 7)
				System.out.println();
			System.out.print("\t" + j);
		}
		for (; j <= days; ++i, ++j) {
			if (0 == i % 7)
				System.out.println();
			System.out.print("\t" + j);
		}
		System.out.println();
	}

	public void ShowTime() {
		System.out.print("\t\t现在是：");
		System.out.print(new SimpleDateFormat("yyyy年MM月dd日").format(new Date())
				+ "           ");
		while (true) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
			System.out.print("\b\b\b\b\b\b\b\b");
			System.out.print(dateFormat.format(new Date()));
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
